package co.edu.udistrital.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author timotheos
 */
public final class XSSUtils {

    private XSSUtils() {
    }

    /**
     * 
     * @param value
     * @return
     */
    public static String clean(String value) {
        if (value != null) {
            value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
            value = value.replaceAll("eval\\((.*)\\)", "");
            value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
            value = value.replaceAll("javascript", "");
            value = value.replaceAll("script", "");
        }
        
        return value;
    }

    /**
     *
     * @param values
     * @return
     */
    public static String[] clean(String[] values) {
        String[] cleaned = new String[0];
        int i = 0;

        if (values != null) {
            cleaned = new String[values.length];
            for (String value : values) {
                cleaned[i++] = clean(value);
            }
        }

        return cleaned;
    }

    /**
     *
     * @param values
     * @return
     */
    public static Map clean(Map parameterMap) {
        Object value = null;
        Map cleaned = new HashMap(0);

        if (parameterMap != null) {
            Set<String> keySet = parameterMap.keySet();

            for (String key : keySet) {
                value = parameterMap.get(key);
                cleaned.put(key, value instanceof String[] ? clean((String[]) value) : clean((String)value));
            }
        }

        return cleaned;
    }
}
